package com.hw.one.dam2.service;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.dao.IPollutionDao;
import com.hw.one.dam2.param.PollutionParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/07/10 11:25.
 */
@Service(value = "pollutionService")
public class PollutionService implements IPollutionService {
    // 污染源数据
    @Autowired
    private IPollutionDao pollutionDao;


    @Override
    public List<Map<String, Object>> findPollutionMinuteNewDataBySNCodes(List<String> snCodeList) {
        PollutionParam pollutionParam = new PollutionParam();
        pollutionParam.setSNCodeList(snCodeList);
        // 查询时间取30分钟之前到现在
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
        String endTime = sdf.format(calendar.getTime());
        calendar.add(Calendar.MINUTE, -30);
        String startTime =  sdf.format(calendar.getTime());
        pollutionParam.setStartTime(startTime);
        pollutionParam.setEndTime(endTime);
        List<Map> smallNewMinuteDataList = pollutionDao.findPollutionMinuteDataByTimeAndSNCodeList(pollutionParam);

        List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();
        Map<String, Object> aqiMap = null;
        for (Map hourAirMap : smallNewMinuteDataList) {
            aqiMap = new HashMap<String, Object>();
            Map<String, Object> monitorData = (Map<String, Object>) hourAirMap.get("MONITORDATA");

            Object ctpm10 = monitorData.get("CTPM10");
            if (null == ctpm10) {
                ctpm10 = monitorData.get("PM10");
                if (null == ctpm10) {
                    ctpm10 = "-";
                }
            }
            aqiMap.put("pm10", ctpm10);

            Object ctpm25 = monitorData.get("CTPM25");
            if (null == ctpm25) {
                ctpm25 = monitorData.get("PM25");
                if (null == ctpm25) {
                    ctpm25 = "-";
                }
            }
            aqiMap.put("pm25", ctpm25);

            String temp = null == monitorData.get("TEMP") ? "" : String.valueOf(monitorData.get("TEMP"));
            if (temp.length() > 5) {
                temp = temp.substring(0,5);
            }
            aqiMap.put("temp", temp);
            String hum = null == monitorData.get("HUM") ? "" : String.valueOf(monitorData.get("HUM"));
            if (hum.length() > 5) {
                hum = hum.substring(0,5);
            }
            aqiMap.put("hum", hum);
            aqiMap.put("windDirection", monitorData.get("WD"));
            aqiMap.put("windSpeed", monitorData.get("WS"));

            aqiMap.put("snCode", hourAirMap.get("SNCODE"));
            aqiMap.put("monitorTime", hourAirMap.get("MONITORTIME"));

            aqiList.add(aqiMap);
        }

        return aqiList;
    }

    @Override
    public List<Map<String, Object>> findPollutionHourNewDataBySNCodes(List<String> snCodeList) {
        List<Map> smallNewMinuteDataList = pollutionDao.findPollutionHourNewDataBySNCodeList(snCodeList);
        List<Map<String, Object>> aqiList = new ArrayList<Map<String, Object>>();

        Map<String, Object> aqiMap = null;
        if (null == smallNewMinuteDataList) {
            return new ArrayList<>();
        }
        for (Map hourAirMap : smallNewMinuteDataList) {
            aqiMap = new HashMap<String, Object>();
            Map<String, Object> monitorData = (Map<String, Object>) hourAirMap.get("MONITORDATA");

            Object ctpm10 = monitorData.get("CTPM10");
            if (null == ctpm10) {
                ctpm10 = monitorData.get("PM10");
                if (null == ctpm10) {
                    ctpm10 = "-";
                }
            }
            aqiMap.put("pm10", ctpm10);

            Object ctpm25 = monitorData.get("CTPM25");
            if (null == ctpm25) {
                ctpm25 = monitorData.get("PM25");
                if (null == ctpm25) {
                    ctpm25 = "-";
                }
            }
            aqiMap.put("pm25", ctpm25);

            String temp = null == monitorData.get("TEMP") ? "" : String.valueOf(monitorData.get("TEMP"));
            if (temp.length() > 5) {
                temp = temp.substring(0,5);
            }
            aqiMap.put("temp", temp);
            String hum = null == monitorData.get("HUM") ? "" : String.valueOf(monitorData.get("HUM"));
            if (hum.length() > 5) {
                hum = hum.substring(0,5);
            }
            aqiMap.put("hum", hum);
            aqiMap.put("windDirection", monitorData.get("WD"));
            aqiMap.put("windSpeed", monitorData.get("WS"));

            aqiMap.put("snCode", hourAirMap.get("SNCODE"));
            aqiMap.put("monitorTime", hourAirMap.get("MONITORTIME"));

            aqiList.add(aqiMap);
        }
        return aqiList;
    }
}
